//
// Created by yfux on 2022/4/19.
//

#ifndef ICANLIVE_AUDIOPLAYER_H
#define ICANLIVE_AUDIOPLAYER_H


#define MAX_AUDIO_FARME_SIZE 48000 * 2

#include "BasePlayer.h"
#include <pthread.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

extern "C" {
#include <libswresample/swresample.h>
#include <libavutil/time.h>
};

class AudioPlayer : public BasePlayer {
    friend void *adecode_t(void *args);

    friend void *aplay_t(void *args);

    friend void bqPlayerCallback(SLAndroidSimpleBufferQueueItf queue, void *pContext);

public:
    AudioPlayer(int channelId, JavaCallHelper *helper, AVCodecContext *avCodecContext,
                AVRational &timeBase);

    virtual ~AudioPlayer();

    void stop() override;

    void decode() override;

    void play() override;

    int getData();

private:
    pthread_t decodeThread;
    pthread_t playThread;
    SwrContext *swrContext = NULL;

    void _decode();

    void _play();

    uint8_t *buffer = NULL;

    void _releaseOpenSL();

private:
    SLObjectItf engineObject = NULL;
    SLEngineItf engineInterface = NULL;
    SLObjectItf outputMixObject = NULL;
    SLObjectItf bqPlayerObject = NULL;
    SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue = NULL;
    SLPlayItf bqPlayerInterface = NULL;
};


#endif //ICANLIVE_AUDIOPLAYER_H
